package com.object;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Random;
import java.util.Scanner;

public class User {
    private String uid;
    private ArrayList<ItemOrder> itemOrders = new ArrayList<>();//商城订单
    private ArrayList<Order> orders = new ArrayList<>();//宠物订单
    PetObj petObj = new PetObj();

    public ArrayList<ItemOrder> getItemOrders() {
        return itemOrders;
    }

    public void setItemOrders(ArrayList<ItemOrder> itemOrders) {
        this.itemOrders = itemOrders;
    }

    private String name;
    private String password;
    private ArrayList<ShoppingCart> shoppingCarts = new ArrayList<>();
    private ArrayList<Item> itemCarts = new ArrayList<>();


    public ArrayList<ShoppingCart> getShoppingCarts() {
        return shoppingCarts;
    }

    public ArrayList<Item> getItemCarts() {
        return itemCarts;
    }

    public void setItemCarts(ArrayList<Item> itemCarts) {
        this.itemCarts = itemCarts;
    }

    public void setShoppingCarts(ArrayList<ShoppingCart> shoppingCarts) {
        this.shoppingCarts = shoppingCarts;
    }

    public ArrayList<Order> getOrders() {
        return orders;
    }

    public void setOrders(ArrayList<Order> orders) {
        this.orders = orders;
    }

    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public User(String name, String password) {
        this.name = name;
        this.password = password;
    }

    public User() {
    }

    public String getTime() {
        //获取时间
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(calendar.getTime());
    }

    //随机生成用户id
    public String newUid(ArrayList<User> users) {
        //生成用户id，
        Random random = new Random();
        StringBuilder uid = new StringBuilder();
        String num = "0123456789";
        String str = "ABCDEFGHIJKL";
        for (int i = 0; i < 3; i++) {
            uid.append(num.charAt(random.nextInt(num.length())));
        }
        uid.append(str.charAt(random.nextInt(str.length())));
        return uid.toString();
    }
    public String newUid(int lenth) {
        //生成用户id，
        Random random = new Random();
        StringBuilder uid = new StringBuilder();
        String num = "0123456789";
        for (int i = 0; i <lenth; i++) {
            uid.append(num.charAt(random.nextInt(num.length())));
        }
        return uid.toString();
    }

    //登录模块
    public User login(ArrayList<User> users, ArrayList<LoginLog> loginLogs, Scanner scanner) {
        if(users.isEmpty()){
            System.out.println("当前没有用户，请先去注册");
            return new User();
        }
        scanner = new Scanner(System.in);
        System.out.println("请输入用户名");
        String name = scanner.next();
        System.out.println("请输入密码");
        String password = scanner.next();
        User loginUser = new User();
        boolean isHad = false;
        for (User user : users) {
            if (user.getName().equals(name) && user.getPassword().equals(password)) {
                //成功的话添加日志
                LoginLog loginLog = new LoginLog();
                loginLog.setUid(user.getUid());
                loginLog.setName(user.getName());
                loginLog.setLoginTime(getTime());
                loginLogs.add(loginLog);
                isHad = true;
                loginUser = user;
                break;
            }
        }
        if (!isHad) {
            System.out.println("请检查用户名和密码");
            login(users, loginLogs, scanner);
        }
        return loginUser;
    }

    //注册模块
    public ArrayList<User> addUser(ArrayList<User> users, Scanner scanner) {
        scanner = new Scanner(System.in);
        User user = new User();
        System.out.println("请输入用户名");
        String name = scanner.next();
        String uid = newUid(users);
        if (users.isEmpty()) {
            System.out.println("请输入密码");
            String password = scanner.next();
            user.setUid(uid);
            user.setName(name);
            user.setPassword(password);
            users.add(user);//添加用户
            System.out.println("注册成功");
            //为空，直接放
        } else {
            //不为空，重名认证
            boolean isHad = false;
            for (User user1 : users) {
                if (user1.getName().equals(name)) {
                    isHad = true;//说明重复
                    break;
                }
            }
            if (isHad) {
                System.out.println("用户名重复，重新输入");
                addUser(users, scanner);
            } else {
                System.out.println("请输入密码");
                String password = scanner.next();
                user.setUid(uid);
                user.setName(name);
                user.setPassword(password);
                users.add(user);//添加用户
                System.out.println("注册成功");

            }
        }
        return users;
    }

    public ArrayList<User> revisePassword(ArrayList<User> users, Scanner scanner) {
        System.out.println("请输入您要找回密码的用户名");
        String name = scanner.next();
        boolean isFlag = false;
        for (User user : users) {
            if (user.getName().equals(name)) {
                System.out.println("请输入您的新密码");
                String password = scanner.next();
                user.setPassword(password);
                System.out.println("密码修改成功，请牢记您的新密码" + password);
                isFlag = true;
                break;
            }
        }
        if (!isFlag) {
            System.out.println("找不到该用户，请重新输入");
            revisePassword(users, scanner);
        }
        return users;
    }

    //打印用户的购物车

}
